<?php
defined('IN_IXDCW') or exit('Access Denied');
/**
 * 合作
 */
class XdcooperModule extends BasicModule{

	/** @var 模块id */
	private $_moduleid;

	/** @var 条目ID */
	private $_itemid;

	/** @var string 表格 */
	private $_table;

	/** @var string 表格数据 */
	private $_tableData;

	/** @var 是否内容分表 */
	private $_split;

	/** @var array 可用字段 */
	private $_fields;


	function __construct() {
		parent::__construct();
		global $table,$moduleid;
		$this->_moduleid  = $moduleid;
		$this->_table     = 'xd_xdcooper';
		$this->_fields = array('catid','username','addtime','content','phone','truename','email','company');
    }
	public function setItemid($itemid) {
		$this->_itemid = $itemid;
	}

	public function getItemid() {
		return $this->_itemid;
	}

	public function getFields() {
		return $this->_fields;
	}
	/**
	 * 是否通过插入检测
	 * @param $post
	 * @return bool
	 */
	function pass($post) {
		if(!is_array($post)) return false;
		if($post['catid']==3238&&empty($post['product'][0])) return $this->err('请填写完整的申请表');
		if($post['catid']!=3238){
			foreach($post as $v){
				if(is_array($v)){
					foreach($v as $c){
						if(empty($c))return $this->err('请填写完整的申请单');
					}
				}else{
					if(empty($v))return $this->err('请填写完整的申请单');
				}
			}
		}
		return true;
	}

	/**
	 * 设置符合内容的值
	 * @param $post
	 * @return array|mixed
	 */
	function set($post) {
		global $DT_TIME,$_username;
		$post['addtime'] = (isset($post['addtime']) && $post['addtime']) ? strtotime($post['addtime']) : $DT_TIME;
		$post['content'] = json_encode($post);
		$post['username'] = $_username;
		return $post;
	}

	/**
	 * 获取一条数据
	 * @return array|void
	 */
	function getOne() {
		$content_table = content_table($this->_moduleid, $this->_itemid, $this->_split, $this->_tableData);
        return $this->_db->getOne("SELECT * FROM {$this->_table} a,{$content_table} c WHERE a.itemid=c.itemid and a.itemid=$this->_itemid");
	}

	/**
	 * 获取用户数据
	 * @return array|void
	 */
	function getUser($userid) {
		return $this->_db->getOne("SELECT * FROM xd_member WHERE userid = {$userid}");
	}

	/**
	 * 获取列表
	 * @param string $condition
	 * @param string $order
	 * @param string $cache
	 * @return array
	 */
	function getList($condition = 'content!=""', $order = 'addtime DESC', $cache = '') {
		global $MOD,$kw,$nums;
		$num = $this->_db->count($this->_table, $condition, $cache);
		$nums=$num;
		$this->_pn->recordsPerPage($this->_pagesize);
		$this->_pn->records($num);
		$sql = "SELECT * FROM {$this->_table}";
		$sql .= $condition ? " WHERE {$condition}" : '';
		$sql .= $order ? " ORDER BY {$order}" : '';
		$sql .= " LIMIT " . ($this->_pn->getPage() - 1) * $this->_pagesize . ',' . $this->_pagesize;
		$lists = $catids = $CATS = array();
		$result = $this->_db->query($sql, $cache);
		while($r = $this->_db->fetchArray($result)) {
			$r['adddate'] = timetodate($r['addtime'], 5);
			$r['adddate'] = timetodate($r['addtime'], 5);
			$catids[$r['catid']] = $r['catid'];
			$lists[] = $r;
		}
		if($catids) {
			$result = $this->_db->query("SELECT catid,catname,linkurl FROM {$this->_db->pre}category WHERE catid IN (".implode(',', $catids).")");
			while($r = $this->_db->fetchArray($result)) {
				$CATS[$r['catid']] = $r;
			}
			if($CATS) {
				foreach($lists as $k=>$v) {
					$lists[$k]['catname'] = $v['catid'] ? $CATS[$v['catid']]['catname'] : '';
					$lists[$k]['caturl'] = $v['catid'] ? $MOD['linkurl'].'?catid='.$v['catid'] : '';
				}
			}
		}
		return $lists;
	}

	/**
	 * 添加数据
	 * @param $post
	 * @return bool
	 */
	function add($post) {
		$post = Str::addSlashes($this->set($post));
		$this->_db->query(Sql::insert($this->_table, $post, $this->_fields));
		$this->_itemid = $this->_db->insertId();
		return $this->_itemid;
	}



}
?>